using System;
using System.Numerics;
using System.Text;
using Oracle.ManagedDataAccess.Types;
using OracleInternal.Core;
using OracleInternal.Json.Common;

namespace OracleInternal.Json.Binary
{
	internal sealed class OsonPrimitiveConversions
	{
		private const int HUNDIGMAX = 66;

		private const int BIGINTARRAYMAX = 54;

		private const int BIGLENMAX = 22;

		private const byte DIGEND = 21;

		private const int LNXSGNBT = 128;

		private const byte LNXDIGS = 20;

		private const int LNXBASE = 100;

		private const int LNXEXPMX = 127;

		private const byte LNXEXPBS = 64;

		private const int LNXEXPMN = 0;

		private const byte ODIGEND = 9;

		internal static int SIZE_TIMESTAMP = 11;

		internal static int SIZE_TIMESTAMP_NOFRAC = 7;

		internal static int SIZE_TIMESTAMPTZ = 13;

		internal static int SIZE_DATE = 7;

		private const int TZ_OFFSET_HOURS = 20;

		private const int TZ_OFFSET_MINUTES = 60;

		private const int MAXYEAR = 9999;

		private const int INTERVAL_BYTE_OFFSET = 60;

		private const uint INTERVAL_INT_OFFSET = 2147483648u;

		private const int SECONDS_PER_DAY = 86400;

		private const int HOURS_PER_DAY = 24;

		private const int MINUTES_PER_HOUR = 60;

		private const int SECONDS_PER_MINUTE = 60;

		private static int Nanos(byte[] bytes)
		{
			if (bytes.Length != SIZE_TIMESTAMP_NOFRAC)
			{
				return GetNanos(bytes, 7);
			}
			return 0;
		}

		private static void YearError(IExceptionFactory f, int year)
		{
			if (year < 1 || year > 9999)
			{
				throw OracleJsonExceptions.BAD_YEAR.Create(f, year);
			}
		}

		internal static bool IsPosInf(byte[] b)
		{
			return _isInf(b);
		}

		internal static bool IsNegInf(byte[] b, int len, int offset)
		{
			if (len != 1)
			{
				return false;
			}
			return b[offset] == 0;
		}

		private static int TruncateZeros(int i)
		{
			int num = i;
			while (num % 10 == 0)
			{
				num /= 10;
			}
			return num;
		}

		private static void AppendInt(StringBuilder result, int n, int i)
		{
			if (n < 0)
			{
				result.Append("-");
				n = -n;
			}
			int num = n;
			while (num > 0)
			{
				num /= 10;
				i--;
			}
			while (i > 0)
			{
				result.Append('0');
				i--;
			}
			if (n != 0)
			{
				int value = n;
				if (i > 0)
				{
					value = TruncateZeros(n);
				}
				result.Append(value);
			}
		}

		internal static string TimestampToString(IExceptionFactory f, byte[] bytes)
		{
			int javaYear = GetJavaYear(bytes[0] & 0xFF, bytes[1] & 0xFF);
			YearError(f, javaYear);
			int n = bytes[2] & 0xFF;
			int n2 = bytes[3] & 0xFF;
			int n3 = (bytes[4] & 0xFF) - 1;
			int n4 = (bytes[5] & 0xFF) - 1;
			int n5 = (bytes[6] & 0xFF) - 1;
			int num = Nanos(bytes);
			StringBuilder stringBuilder = new StringBuilder(27);
			AppendInt(stringBuilder, javaYear, 4);
			stringBuilder.Append("-");
			AppendInt(stringBuilder, n, 2);
			stringBuilder.Append("-");
			AppendInt(stringBuilder, n2, 2);
			stringBuilder.Append("T");
			AppendInt(stringBuilder, n3, 2);
			stringBuilder.Append(":");
			AppendInt(stringBuilder, n4, 2);
			stringBuilder.Append(":");
			AppendInt(stringBuilder, n5, 2);
			stringBuilder.Append(".");
			if (num > 0)
			{
				if (num % 1000 > 0)
				{
					AppendInt(stringBuilder, num, 9);
				}
				else if (num % 1000000 > 0)
				{
					AppendInt(stringBuilder, num / 1000, 6);
				}
				else
				{
					AppendInt(stringBuilder, num / 1000000, 3);
				}
			}
			else if (num == 0)
			{
				stringBuilder.Append("000000");
			}
			return stringBuilder.ToString();
		}

		internal static string TimestampTZToString(IExceptionFactory f, byte[] bytes)
		{
			int javaYear = GetJavaYear(bytes[0] & 0xFF, bytes[1] & 0xFF);
			YearError(f, javaYear);
			int n = bytes[2] & 0xFF;
			int num = bytes[3] & 0xFF;
			int num2 = (bytes[4] & 0xFF) - 1;
			int num3 = (bytes[5] & 0xFF) - 1;
			int n2 = (bytes[6] & 0xFF) - 1;
			int num4 = Nanos(bytes);
			int num5 = (bytes[11] & 0xFF) - 20;
			int num6 = (bytes[12] & 0xFF) - 60;
			num2 += num5;
			num3 += num6;
			if (num3 > 60)
			{
				num2++;
				num3 %= 60;
			}
			else if (num3 < 0)
			{
				num2--;
				num3 += 60;
			}
			if (num2 > 24)
			{
				num++;
				num2 %= 24;
			}
			else if (num2 < 0)
			{
				num--;
				num2 = 24 + num2;
			}
			StringBuilder stringBuilder = new StringBuilder(27);
			AppendInt(stringBuilder, javaYear, 4);
			stringBuilder.Append("-");
			AppendInt(stringBuilder, n, 2);
			stringBuilder.Append("-");
			AppendInt(stringBuilder, num, 2);
			stringBuilder.Append("T");
			AppendInt(stringBuilder, num2, 2);
			stringBuilder.Append(":");
			AppendInt(stringBuilder, num3, 2);
			stringBuilder.Append(":");
			AppendInt(stringBuilder, n2, 2);
			stringBuilder.Append(".");
			if (num4 > 0)
			{
				if (num4 % 1000 > 0)
				{
					AppendInt(stringBuilder, num4, 9);
				}
				else if (num4 % 1000000 > 0)
				{
					AppendInt(stringBuilder, num4 / 1000, 6);
				}
				else
				{
					AppendInt(stringBuilder, num4 / 1000000, 3);
				}
			}
			else if (num4 == 0)
			{
				stringBuilder.Append("000000");
			}
			if (num5 > 0)
			{
				stringBuilder.Append("+");
			}
			if (num5 == 0 && num6 == 0)
			{
				stringBuilder.Append("Z");
			}
			else
			{
				AppendInt(stringBuilder, num5, 2);
				stringBuilder.Append(":");
				num6 = Math.Abs(num6);
				AppendInt(stringBuilder, num6, 2);
			}
			return stringBuilder.ToString();
		}

		internal static DateTime DateToLocalDateTime(IExceptionFactory f, byte[] bytes)
		{
			int javaYear = GetJavaYear(bytes[0] & 0xFF, bytes[1] & 0xFF);
			YearError(f, javaYear);
			int month = bytes[2] & 0xFF;
			int day = bytes[3] & 0xFF;
			int hour = (bytes[4] & 0xFF) - 1;
			int minute = (bytes[5] & 0xFF) - 1;
			int second = (bytes[6] & 0xFF) - 1;
			return new DateTime(javaYear, month, day, hour, minute, second);
		}

		internal static DateTime TimestampToLocalDateTime(IExceptionFactory f, byte[] bytes)
		{
			int javaYear = GetJavaYear(bytes[0] & 0xFF, bytes[1] & 0xFF);
			YearError(f, javaYear);
			int month = bytes[2] & 0xFF;
			int day = bytes[3] & 0xFF;
			int hour = (bytes[4] & 0xFF) - 1;
			int minute = (bytes[5] & 0xFF) - 1;
			int second = (bytes[6] & 0xFF) - 1;
			int millisecond = Nanos(bytes);
			return new DateTime(javaYear, month, day, hour, minute, second, millisecond);
		}

		internal static byte[] ToOracleDate(IExceptionFactory f, DateTime local)
		{
			int year = local.Year;
			YearError(f, year);
			byte[] array = new byte[SIZE_DATE];
			array[0] = (byte)(year / 100 + 100);
			array[1] = (byte)(year % 100 + 100);
			array[2] = (byte)local.Month;
			array[3] = (byte)local.Day;
			array[4] = (byte)(local.Hour + 1);
			array[5] = (byte)(local.Minute + 1);
			array[6] = (byte)(local.Second + 1);
			return array;
		}

		internal static byte[] ToOracleTimestamp(IExceptionFactory f, DateTime local)
		{
			int year = local.Year;
			YearError(f, year);
			int num = local.Millisecond * 1000;
			byte[] array = new byte[(num == 0) ? SIZE_TIMESTAMP_NOFRAC : SIZE_TIMESTAMP];
			array[0] = (byte)(year / 100 + 100);
			array[1] = (byte)(year % 100 + 100);
			array[2] = (byte)local.Month;
			array[3] = (byte)local.Day;
			array[4] = (byte)(local.Hour + 1);
			array[5] = (byte)(local.Minute + 1);
			array[6] = (byte)(local.Second + 1);
			if (num > 0)
			{
				array[7] = (byte)(num >> 24);
				array[8] = (byte)((uint)(num >> 16) & 0xFFu);
				array[9] = (byte)((uint)(num >> 8) & 0xFFu);
				array[10] = (byte)((uint)num & 0xFFu);
			}
			return array;
		}

		internal static string DateToString(IExceptionFactory f, byte[] bytes)
		{
			int javaYear = GetJavaYear(bytes[0] & 0xFF, bytes[1] & 0xFF);
			YearError(f, javaYear);
			int n = bytes[2] & 0xFF;
			int n2 = bytes[3] & 0xFF;
			int n3 = (bytes[4] & 0xFF) - 1;
			int n4 = (bytes[5] & 0xFF) - 1;
			int n5 = (bytes[6] & 0xFF) - 1;
			StringBuilder stringBuilder = new StringBuilder(27);
			AppendInt(stringBuilder, javaYear, 4);
			stringBuilder.Append("-");
			AppendInt(stringBuilder, n, 2);
			stringBuilder.Append("-");
			AppendInt(stringBuilder, n2, 2);
			stringBuilder.Append("T");
			AppendInt(stringBuilder, n3, 2);
			stringBuilder.Append(":");
			AppendInt(stringBuilder, n4, 2);
			stringBuilder.Append(":");
			AppendInt(stringBuilder, n5, 2);
			return stringBuilder.ToString();
		}

		internal static TimeSpan IntervalDSToDuration(byte[] raw)
		{
			long num = GetDaysFromIntervalDS(raw);
			int hoursFromIntervalDS = GetHoursFromIntervalDS(raw);
			int minutesFromIntervalDS = GetMinutesFromIntervalDS(raw);
			int secondsFromIntervalDS = GetSecondsFromIntervalDS(raw);
			GetNanosFromIntervalDS(raw);
			return TimeSpan.FromSeconds(num * 86400 + (long)hoursFromIntervalDS * 3600L + (long)minutesFromIntervalDS * 60L + secondsFromIntervalDS);
		}

		internal static string SerializeIntervalDS(IExceptionFactory f, byte[] raw)
		{
			long num = GetDaysFromIntervalDS(raw);
			long num2 = GetHoursFromIntervalDS(raw);
			long num3 = GetMinutesFromIntervalDS(raw);
			long num4 = GetSecondsFromIntervalDS(raw);
			long nanosFromIntervalDS = GetNanosFromIntervalDS(raw);
			int num5 = (int)(nanosFromIntervalDS / 1000);
			nanosFromIntervalDS %= 1000;
			if (num < 0 || num2 < 0 || num3 < 0 || num4 < 0 || num5 < 0 || nanosFromIntervalDS < 0)
			{
				throw OracleJsonExceptions.NOT_IMPLEMENTED.Create(f);
			}
			bool flag = num2 > 0 || num3 > 0 || num4 > 0 || num5 > 0 || nanosFromIntervalDS > 0;
			StringBuilder stringBuilder = new StringBuilder();
			stringBuilder.Append("P");
			if (num > 0 || !flag)
			{
				stringBuilder.Append(num);
				stringBuilder.Append("D");
			}
			if (flag)
			{
				stringBuilder.Append("T");
				if (num2 > 0)
				{
					stringBuilder.Append(num2).Append("H");
				}
				if (num3 > 0)
				{
					stringBuilder.Append(num3).Append("M");
				}
				if (num4 > 0 || num5 > 0 || nanosFromIntervalDS > 0)
				{
					stringBuilder.Append(num4);
					if (num5 > 0 || nanosFromIntervalDS > 0)
					{
						string text = num5.ToString();
						stringBuilder.Append(".");
						for (int i = 0; i < 6 - text.Length; i++)
						{
							stringBuilder.Append("0");
						}
						stringBuilder.Append(num5);
						if (nanosFromIntervalDS > 0)
						{
							string text2 = nanosFromIntervalDS.ToString();
							for (int j = 0; j < 3 - text2.Length; j++)
							{
								stringBuilder.Append("0");
							}
							stringBuilder.Append(nanosFromIntervalDS);
						}
					}
					stringBuilder.Append("S");
				}
			}
			return stringBuilder.ToString();
		}

		internal static byte[] DurationToIntervalDS(TimeSpan duration)
		{
			long num = duration.Seconds;
			long num2 = num / 86400;
			long num3 = duration.Hours % 24;
			long num4 = duration.Minutes % 60;
			long num5 = num % 60;
			int num6 = duration.Milliseconds * 1000;
			int num7 = (int)num2;
			if (num7 != num2)
			{
				throw new ArgumentException();
			}
			byte[] array = new byte[11];
			WriteIntervalInt(num7, 0, array);
			array[4] = (byte)(num3 + 60);
			array[5] = (byte)(num4 + 60);
			array[6] = (byte)(num5 + 60);
			WriteIntervalInt(num6, 7, array);
			return array;
		}

		private static void WriteIntervalInt(long value, int offset, byte[] bytes)
		{
			value += 2147483648u;
			bytes[offset++] = (byte)((0xFF000000u & value) >> 24);
			bytes[offset++] = (byte)((0xFF0000 & value) >> 16);
			bytes[offset++] = (byte)((0xFF00 & value) >> 8);
			bytes[offset++] = (byte)(0xFF & value);
		}

		private static int GetDaysFromIntervalDS(byte[] bytes)
		{
			return (((bytes[0] & 0xFF) << 24) | ((bytes[1] & 0xFF) << 16) | ((bytes[2] & 0xFF) << 8) | (bytes[3] & 0xFF)) - int.MinValue;
		}

		private static int GetHoursFromIntervalDS(byte[] bytes)
		{
			return bytes[4] - 60;
		}

		private static int GetMinutesFromIntervalDS(byte[] bytes)
		{
			return bytes[5] - 60;
		}

		private static int GetSecondsFromIntervalDS(byte[] bytes)
		{
			return bytes[6] - 60;
		}

		private static long GetNanosFromIntervalDS(byte[] bytes)
		{
			return (((bytes[7] & 0xFF) << 24) | ((bytes[8] & 0xFF) << 16) | ((bytes[9] & 0xFF) << 8) | (bytes[10] & 0xFF)) - int.MinValue;
		}

		internal static Period IntervalYMToPeriod(byte[] raw)
		{
			int yearFromIntervalYM = GetYearFromIntervalYM(raw);
			int monthFromIntervalYM = GetMonthFromIntervalYM(raw);
			return new Period(yearFromIntervalYM, monthFromIntervalYM);
		}

		internal static byte[] PeriodToIntervalYM(IExceptionFactory f, Period p)
		{
			int years = p.Years;
			int months = p.Months;
			if (months > 11)
			{
				throw OracleJsonExceptions.NOT_IMPLEMENTED.Create(f);
			}
			if (years < 0 || months < 0)
			{
				throw OracleJsonExceptions.NOT_IMPLEMENTED.Create(f);
			}
			if (p.Days != 0)
			{
				throw OracleJsonExceptions.NO_DAYS_ALLOWED.Create(f);
			}
			byte[] array = new byte[5];
			WriteIntervalInt(p.Years, 0, array);
			array[4] = (byte)(p.Months + 60);
			return array;
		}

		private static int GetMonthFromIntervalYM(byte[] raw)
		{
			return raw[4] - 60;
		}

		private static int GetYearFromIntervalYM(byte[] raw)
		{
			return (int)((((raw[0] & 0xFF) << 24) | ((raw[1] & 0xFF) << 16) | ((raw[2] & 0xFF) << 8) | (raw[3] & 0xFF)) - 2147483648u);
		}

		internal static string SerializeIntervalYM(IExceptionFactory f, byte[] raw)
		{
			int yearFromIntervalYM = GetYearFromIntervalYM(raw);
			int monthFromIntervalYM = GetMonthFromIntervalYM(raw);
			if (yearFromIntervalYM < 0 || monthFromIntervalYM < 0)
			{
				throw OracleJsonExceptions.NOT_IMPLEMENTED.Create(f);
			}
			StringBuilder stringBuilder = new StringBuilder();
			stringBuilder.Append('P');
			if (yearFromIntervalYM > 0 || monthFromIntervalYM == 0)
			{
				stringBuilder.Append(yearFromIntervalYM).Append('Y');
			}
			if (monthFromIntervalYM > 0)
			{
				stringBuilder.Append(monthFromIntervalYM).Append('M');
			}
			return stringBuilder.ToString();
		}

		internal static byte[] DateTimeToTimestamp(IExceptionFactory f, DateTime instant)
		{
			return ToOracleTimestamp(f, instant.ToUniversalTime());
		}

		internal static byte[] DateTimeToDate(IExceptionFactory f, DateTime instant)
		{
			return ToOracleDate(f, instant.ToUniversalTime());
		}

		private static int GetNanos(byte[] buffer, int off)
		{
			return ((buffer[off] & 0xFF) << 24) | ((buffer[off + 1] & 0xFF) << 16) | ((buffer[off + 2] & 0xFF) << 8) | (buffer[off + 3] & 0xFF & 0xFF);
		}

		private static int GetJavaYear(int cent, int decade)
		{
			int num = (cent - 100) * 100 + (decade - 100);
			if (num < 0)
			{
				num++;
			}
			return num;
		}

		internal static byte[] DoubleToCanonicalFormatBytes(double _d)
		{
			double num = _d;
			if (num == 0.0)
			{
				num = 0.0;
			}
			else if (num != _d)
			{
				num = double.NaN;
			}
			long num2 = BitConverter.DoubleToInt64Bits(num);
			byte[] array = new byte[8];
			int num3 = (int)num2;
			int num4 = (int)(num2 >> 32);
			int num5 = num3;
			num3 >>= 8;
			int num6 = num3;
			num3 >>= 8;
			int num7 = num3;
			num3 >>= 8;
			int num8 = num3;
			int num9 = num4;
			int num11;
			int num10;
			int num12 = (num11 = (num10 = num4 >> 8) >> 8) >> 8;
			if ((num12 & 0x80) == 0)
			{
				num12 |= 0x80;
			}
			else
			{
				num12 = ~num12;
				num11 = ~num11;
				num10 = ~num10;
				num9 = ~num9;
				num8 = ~num8;
				num7 = ~num7;
				num6 = ~num6;
				num5 = ~num5;
			}
			array[7] = (byte)num5;
			array[6] = (byte)num6;
			array[5] = (byte)num7;
			array[4] = (byte)num8;
			array[3] = (byte)num9;
			array[2] = (byte)num10;
			array[1] = (byte)num11;
			array[0] = (byte)num12;
			return array;
		}

		internal static double CanonicalFormatBytesToDouble(byte[] b)
		{
			int num = b[0];
			int num2 = b[1];
			int num3 = b[2];
			int num4 = b[3];
			int num5 = b[4];
			int num6 = b[5];
			int num7 = b[6];
			int num8 = b[7];
			if (((uint)num & 0x80u) != 0)
			{
				num &= 0x7F;
				num2 &= 0xFF;
				num3 &= 0xFF;
				num4 &= 0xFF;
				num5 &= 0xFF;
				num6 &= 0xFF;
				num7 &= 0xFF;
				num8 &= 0xFF;
			}
			else
			{
				num = ~num & 0xFF;
				num2 = ~num2 & 0xFF;
				num3 = ~num3 & 0xFF;
				num4 = ~num4 & 0xFF;
				num5 = ~num5 & 0xFF;
				num6 = ~num6 & 0xFF;
				num7 = ~num7 & 0xFF;
				num8 = ~num8 & 0xFF;
			}
			int num9 = (num << 24) | (num2 << 16) | (num3 << 8) | num4;
			int num10 = (num5 << 24) | (num6 << 16) | (num7 << 8) | num8;
			return BitConverter.Int64BitsToDouble(((long)num9 << 32) | (num10 & 0xFFFFFFFFu));
		}

		internal static byte[] FloatToCanonicalFormatBytes(float _f)
		{
			float num = _f;
			if (num == 0f)
			{
				num = 0f;
			}
			else if (num != _f)
			{
				num = float.NaN;
			}
			int num2 = (int)BitConverter.DoubleToInt64Bits(num);
			byte[] array = new byte[4];
			int num3 = num2;
			int num5;
			int num4;
			int num6 = (num5 = (num4 = num2 >> 8) >> 8) >> 8;
			if ((num6 & 0x80) == 0)
			{
				num6 |= 0x80;
			}
			else
			{
				num6 = ~num6;
				num5 = ~num5;
				num4 = ~num4;
				num3 = ~num3;
			}
			array[3] = (byte)num3;
			array[2] = (byte)num4;
			array[1] = (byte)num5;
			array[0] = (byte)num6;
			return array;
		}

		internal static float CanonicalFormatBytesToFloat(byte[] b)
		{
			int num = b[0];
			int num2 = b[1];
			int num3 = b[2];
			int num4 = b[3];
			if (((uint)num & 0x80u) != 0)
			{
				num &= 0x7F;
				num2 &= 0xFF;
				num3 &= 0xFF;
				num4 &= 0xFF;
			}
			else
			{
				num = ~num & 0xFF;
				num2 = ~num2 & 0xFF;
				num3 = ~num3 & 0xFF;
				num4 = ~num4 & 0xFF;
			}
			return (float)BitConverter.Int64BitsToDouble((num << 24) | (num2 << 16) | (num3 << 8) | num4);
		}

		internal static byte[] GetOracleNumberFromDecimal(decimal dec)
		{
			DecimalConv.GetBytes(dec, out var bytes);
			if (bytes.Length == 22)
			{
				int num = bytes[0];
				byte[] array = new byte[num];
				Array.Copy(bytes, 1, array, 0, num);
				return array;
			}
			return bytes;
		}

		internal static byte[] ToNumber(decimal BigDecNum)
		{
			return GetOracleNumberFromDecimal(BigDecNum);
		}

		internal static byte[] ToNumber(BigInteger BigIntNum)
		{
			return GetOracleNumberFromDecimal((decimal)BigIntNum);
		}

		internal static byte[] ToNumber(long value)
		{
			return OracleNumberCore.lnxmin(value);
		}

		private static byte[] _makeZero()
		{
			return new byte[1]
			{
				128
			};
		}

		private static byte[] _toLnxFmt(byte[] num, bool pos)
		{
			int num2 = num.Length;
			byte[] array;
			if (pos)
			{
				array = new byte[num2];
				array[0] = (byte)(num[0] + 128 + 64 + 1);
				for (int i = 1; i < num2; i++)
				{
					array[i] = (byte)(num[i] + 1);
				}
			}
			else
			{
				array = ((num2 - 1 >= 20) ? new byte[num2] : new byte[num2 + 1]);
				array[0] = (byte)(~(num[0] + 128 + 64 + 1));
				int i;
				for (i = 1; i < num2; i++)
				{
					array[i] = (byte)(101 - num[i]);
				}
				if (i <= 20)
				{
					array[i] = 102;
				}
			}
			return array;
		}

		private static byte[] ToNumber(int value)
		{
			return OracleNumberCore.lnxmin(value);
		}

		private static int Log100(long longNum)
		{
			if (longNum <= -10000000000L)
			{
				if (longNum <= -1000000000000L)
				{
					if (longNum <= -100000000000000L)
					{
						if (longNum <= -10000000000000000L)
						{
							if (longNum <= -1000000000000000000L)
							{
								return 9;
							}
							return 8;
						}
						return 7;
					}
					return 6;
				}
				return 5;
			}
			return 4;
		}

		private static void LnxminEncodeP1(int digit, int exponent, byte[] dst, int offset)
		{
			dst[offset] = (byte)(193 + exponent);
			dst[offset + 1] = (byte)(1 + digit);
		}

		private static void LnxminEncodeN1(int digit, int exponent, byte[] dst, int offset)
		{
			dst[offset] = (byte)(62 - exponent);
			dst[offset + 1] = (byte)(101 - digit);
			dst[offset + 2] = 102;
		}

		private static void LnxminEncodeP(long val, int exponent, int digits, int leastDigit, byte[] dst, int offset)
		{
			dst[offset] = (byte)(193 + exponent);
			int num = offset + digits + 1;
			dst[num--] = (byte)(1 + leastDigit);
			while (digits-- > 4)
			{
				long num2 = val / 100;
				dst[num--] = (byte)(1 + num2 * 100 - val);
				val = num2;
			}
			int num3 = (int)val;
			while (num3 <= -100)
			{
				int num4 = num3 / 100;
				dst[num--] = (byte)(1 + num4 * 100 - num3);
				num3 = num4;
			}
			dst[num] = (byte)(1 - num3);
		}

		private static void LnxminEncodeN(long val, int exponent, int digits, int leastDigit, byte[] dst, int offset)
		{
			dst[offset] = (byte)(64 - (2 + exponent));
			int num = offset + digits + 2;
			dst[num--] = 102;
			dst[num--] = (byte)(101 - leastDigit);
			while (digits-- > 4)
			{
				long num2 = val / 100;
				dst[num--] = (byte)(101 - (num2 * 100 - val));
				val = num2;
			}
			int num3 = (int)val;
			while (num3 <= -100)
			{
				int num4 = num3 / 100;
				dst[num--] = (byte)(101 - (num4 * 100 - num3));
				num3 = num4;
			}
			dst[num] = (byte)(101 + num3);
		}

		internal static decimal ToBigDecimal(byte[] num)
		{
			return DecimalConv.GetDecimal(num, 0, num.Length);
		}

		private static bool _isZero(byte[] num)
		{
			if (num[0] == 128 && num.Length == 1)
			{
				return true;
			}
			return false;
		}

		private static bool _isInf(byte[] num)
		{
			if ((num.Length == 2 && num[0] == byte.MaxValue && num[1] == 101) || (num[0] == 0 && num.Length == 1))
			{
				return true;
			}
			return false;
		}

		private static bool _isPositive(byte[] num)
		{
			if ((num[0] & 0x80u) != 0)
			{
				return true;
			}
			return false;
		}

		private static byte[] _fromLnxFmt(byte[] num)
		{
			int num2 = num.Length;
			byte[] array;
			if (_isPositive(num))
			{
				array = new byte[num2];
				array[0] = (byte)((num[0] & -129) - 65);
				for (int i = 1; i < num2; i++)
				{
					array[i] = (byte)(num[i] - 1);
				}
			}
			else
			{
				array = ((num2 - 1 != 20 || num[num2 - 1] == 102) ? new byte[num2 - 1] : new byte[num2]);
				array[0] = (byte)((~num[0] & -129) - 65);
				for (int i = 1; i < array.Length; i++)
				{
					array[i] = (byte)(101 - num[i]);
				}
			}
			return array;
		}
	}
}
